<template>
   <div class="login">
    <div class="main">
      <h2 class="title">zero-x办公软件</h2>
      <el-form
        ref="loginFormRef"
        v-loading="loading"
        element-loading-text="正在登录..."
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.8)"
        class="login-form"
        :model="loginForm"
        :rules="loginRules"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            prefix-icon="iconfont icon-yonghu"
            auto-complete="off"
            placeholder="账号"
          />
        </el-form-item>
        <el-form-item prop="password" v-if="visiblePwd">
          <el-input
            v-model="loginForm.password"
            type="password"
            prefix-icon="iconfont icon-mima"
            auto-complete="off"
            placeholder="密码"
          >
            <i
              slot="suffix"
              title="显示密码"
              style="cursor:pointer;"
              class="iconfont icon-yanjing_yincang"
              @click="changePass('show')"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="password" v-else>
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-mima"
            auto-complete="off"
            placeholder="密码"
          >
            <i
              slot="suffix"
              title="隐藏密码"
              style="cursor:pointer;"
              class="iconfont icon-yanjing_xianshi"
              @click="changePass('hide')"
            />
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            v-model="loginForm.code"
            prefix-icon="iconfont icon-yanzhengma"
            auto-complete="off"
            placeholder="验证码"
            style="width: 58%"
          />
          <div class="login-code">
            <img :src="captchaUrl" class="login-code-img" @click="updateCaptcha">
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <el-button
            size="medium"
            type="primary"
            style="width:100%;"
            @click="submitLogin"
          >登 录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2019-2022 零九科技 All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
// 导入阿里巴巴矢量图样式表
import { getCaptcha } from '@/api/user'
import '../../assets/fonts/iconfont.css'

export default {
  name: 'Login',
  data() {
    // 这里存放数据
    return {
      captchaUrl: '',
      loginForm: {
        username: '', // 用户名
        password: '', // 密码
        rememberMe: false, // 记住我
        code: '' // 验证码
      },
      visiblePwd: true,
      loginRules: {
        username: [
          { required: true, trigger: 'blur', message: '用户名不能为空' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, trigger: 'blur', message: '密码不能为空' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        code: [{ required: true, trigger: 'blur', message: '验证码不能为空' }]
      },
      // false表示不是正在加载中，true表示正在加载中
      loading: false
    }
  },
  watch: {
  },
  created() {
    // 获取验证码
    this.updateCaptcha()
  },
  methods: {
    // 显示密码
    changePass(value) {
      this.visiblePwd = !(value === 'show')
    },
    // 验证码图片的点击事件，实现更新验证码图片
    updateCaptcha() {
      getCaptcha().then(response => {
        this.captchaUrl = `data:image/gif;base64,${response.img}`
      })
    },
    // 登录提交
    submitLogin() {
      this.$refs.loginFormRef.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            // {path: this.redirect || '/'} 这种是函数参数默认值，判断this.redirect是否为空
            // 如果是空的，给默认值 '/'
            this.$router.push({ path: this.redirect || '/' })
            this.loading = false
          }).catch((error) => {
            console.log(error)
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 160px 35px 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>


<style lang="less" scoped>
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    /* height: 705px;*/
    background: url("../../assets/images/login-background.jpg") no-repeat;
    background-size: 100% 100%;
    position: relative;
  }
  .main {
    position: absolute;
    height: 50%;
    top: 19%;
    right: 10%;
  }
  .title {
    margin: 0px auto 30px auto;
    text-align: center;
    color: #fff;
  }

  .login-form {
    background-image: url("../../assets/images/background_login.png");
    background-size: 100% 100%;
    border-radius: 6px;
    width: 340px;
    padding: 45px;
  }
  .login-form .el-checkbox {
    color: #fff;
    float: left;
    padding-left: 4px;
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
  .login-form .el-input {
    height: 38px;
  }
  .login-form .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: gray;
    cursor: pointer;
    user-select: none;
  }
  // .login-form input {
  /*使用/deep/ ，两边要加空格，来做样式穿透，来提高此样式的优先级别*/
  .el-input /deep/ .el-input__inner{
    background: #597277;
    height: 38px;
    color: #fff;
  }
  .login-code {
    width: 39%;
    height: 38px;
    float: right;
  }
  .login-code img {
    cursor: pointer;
    vertical-align: middle;
  }
  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }
  .login-code-img {
    height: 38px;
  }
</style>



